import * as vscode from 'vscode';
import { BugItem, MyBugProvider } from './data-provider/mybug';
import { MyTaskProvider, TaskItem } from './data-provider/mytask';
import { initProjectTokenCfg } from './utils/tokenUtils';
import { createThread } from './comments/command';
import * as threadManager from "./comments/threadManger";
import { EntryItem, EntryProvider } from './data-provider/entry';

export function activate(context: vscode.ExtensionContext) {

	vscode.commands.registerCommand('linksaas.initCfg', () => initProjectTokenCfg());

	const myBugProvider = new MyBugProvider();
	vscode.window.registerTreeDataProvider("myBugList", myBugProvider);
	vscode.commands.registerCommand('myBugList.sync', () => myBugProvider.sync());
	vscode.commands.registerCommand('myBugList.showItem', (item: BugItem) => item.show());
	vscode.commands.registerCommand('myBugList.shortNote', (item: BugItem) => item.shortNote());


	const myTaskProvider = new MyTaskProvider();
	vscode.window.registerTreeDataProvider("myTaskList", myTaskProvider);
	vscode.commands.registerCommand('myTaskList.sync', () => myTaskProvider.sync());
	vscode.commands.registerCommand('myTaskList.showItem', (item: TaskItem) => item.show());
	vscode.commands.registerCommand('myTaskList.shortNote', (item: TaskItem) => item.shortNote());

	const entryProvider = new EntryProvider();
	vscode.window.registerTreeDataProvider("entryList", entryProvider);
	vscode.commands.registerCommand('entryList.sync', () => entryProvider.sync());
	vscode.commands.registerCommand('entryList.showItem', (item: EntryItem) => item.show());


	vscode.commands.registerCommand('codeComment.newThread', () => createThread());
	const commentController = vscode.comments.createCommentController("linksaas", "linksaas comment controller");
	threadManager.init(commentController);
	commentController.commentingRangeProvider = {
		provideCommentingRanges(document: vscode.TextDocument, _token: vscode.CancellationToken): vscode.ProviderResult<vscode.Range[]> {
			threadManager.onDocChange(document);
			return null;
		}
	};
	vscode.commands.registerCommand("codeComment.syncThread", (thread: vscode.CommentThread) => threadManager.syncThread(thread));
	vscode.commands.registerCommand("codeComment.add", (reply: vscode.CommentReply) => threadManager.addComment(reply));
	vscode.commands.registerCommand("codeComment.update", (comment: threadManager.CodeComment) => threadManager.setEditComment(comment));
	vscode.commands.registerCommand("codeComment.remove", (comment: threadManager.CodeComment) => threadManager.removeComment(comment));
	vscode.commands.registerCommand("codeComment.cancelSave", (comment: threadManager.CodeComment) => threadManager.cancelSaveComment(comment));
	vscode.commands.registerCommand("codeComment.save", (comment: threadManager.CodeComment) => threadManager.saveComment(comment));
}

export function deactivate() { }
